<?php
namespace app\api\controller;
use app\common\controller\ApiBase;
use think\facade\Request;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Session;
use app\api\model\Customer as Kehu;
use app\common\model\User;
use app\user\model\CustomerTrack;
use app\user\model\CustomerOrder;
use app\user\model\Notice;

class Customer extends ApiBase
{
    //protected $noAuth = ['index','edit_user', 'update_userinfo','get_code','logout'];
    protected $noAuth = ['json_track','json_order'];
    protected function _initialize()
    {
        parent::_initialize();
        
    }
    //个人中心
    public function index()
    {
        $customer = new Kehu();
        
        $list = $customer->with(['classify','comefrom','status','user'])->where('user_id', UserId())->order('update_time desc,id desc')->paginate();
        $this->result(['lists' => $list,'islogin' => '1'], '0', '获取成功', 'json');
    }
    //新增
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $param['user_id'] = UserId();
            //验证手机号
            $customer = Kehu::where('mobile',$param['mobile'])->find();
            if($customer){
                $this->result('', '-1', '手机号已存在', 'json');
            }
            Kehu::create($param);
            insert_api_log('新增客户:'.$param['name']);
            $this->result('', '0', '操作成功', 'json');
        }
        
        $classify = get_customer_config('classify');
        $comefrom = get_customer_config('comefrom');
        $status = get_customer_config('status');
        $data = [
            'classify'  =>  $classify,
            'comefrom'  =>  $comefrom,
            'status'    =>  $status,
        ];
        $this->result($data, '0', '操作成功', 'json');
    }
    //我的客户
    public function detail($id) 
    {
        $customer = new Kehu();
        $data = $customer->with(['classify','comefrom','status','user'])->where('id',$id)->find();
        //判断是否处理通知
        $notice = input('notice');
        if($notice){
            Notice::where('id',$notice)->update(['status'=>'1']);
        }
        $this->result($data, '0', '操作成功', 'json');
    }
    //JSON 获取客户回访
    public function json_track($customer_id,$limit)
    {
        $list = CustomerTrack::with(['user'])->where('customer_id',$customer_id)->order('update_time desc,id desc')->paginate($limit);
        $this->result(['lists'=>$list], '0', '操作成功', 'json');
    }
    //JSON 获取客户交易
    public function json_order($customer_id,$limit)
    {
        $list = CustomerOrder::with(['user','classify'])->where('customer_id',$customer_id)->order('update_time desc,id desc')->paginate($limit);
        $this->result(['lists'=>$list], '0', '操作成功', 'json');
    }
    //编辑
    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证手机号
            $customer = Kehu::where('mobile',$param['mobile'])->where('id','<>',$param['id'])->find();
            if($customer){
                $this->result('', '-1', '手机号已存在', 'json');
            }
            Kehu::update($param,['id'=>$param['id']]);
            insert_api_log('修改了客户资料');
            $this->result('', '0', '操作成功', 'json');
        }
        $data = Kehu::where('id',$id)->find();
        $classify = get_customer_config('classify');
        $comefrom = get_customer_config('comefrom');
        $status = get_customer_config('status');
        $data = [
            'data'      =>  $data,
            'classify'  =>  $classify,
            'comefrom'  =>  $comefrom,
            'status'    =>  $status,
        ];
        $this->result($data, '0', '操作成功', 'json');
    }
    //删除
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Kehu::destroy($param['id']);
            insert_api_log('删除了客户');
            $this->result('', '0', '删除成功', 'json');
        }
    }
    //新增回访
    public function track_add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $param['user_id'] = UserId();
            CustomerTrack::create($param);
            //更新客户信息表
            Kehu::updateTime($param['customer_id']);
            insert_api_log('新增回访');
            $this->result('', '0', '操作成功', 'json');
        }
    }
    //新增销售
    public function order_add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $param['user_id'] = UserId();
            CustomerOrder::create($param);
            //更新客户信息表
            Kehu::updateTime($param['customer_id']);
            insert_api_log('新增销售');
            $this->result('', '0', '操作成功', 'json');
        }
        $classify = \app\user\model\CustomerOrderClassify::get_lists();
        $this->result(['classify'=>$classify], '0', '操作成功', 'json');
    }
    
}
